import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../components/Home.vue'
import Login from '../components/Login.vue'
import Data from '../components/Data.vue'
import Book from '../components/Book.vue'
import Resources from '../components/Resources.vue'
import User from '../components/User.vue'
import Notfound from '../components/Notfound.vue'
import Welcome from '../components/Welcome.vue'
import Personal from '../components/Personal.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect: "book",
    children: [
      { path: "/data", component: Data },
      { path: "/book", component: Book },
      { path: "/resources", component: Resources },
      { path: "/user", component: User },
      { path: "/welcome", component: Welcome },
      { path: "/personal", component: Personal },
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: "/404",
    name: "Notfound",
    component: Notfound
  }, {
    path: "*", // 此处需特别注意置于最底部
    redirect: "/404"
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 导航守卫
router.beforeEach((to, form, next) => {
  if(to.path === "/login"){
    if(localStorage.getItem("token"))return next("/welcome")
    next()
  }else{
    if(localStorage.getItem("token"))return next()
      next("/login")
  }
})
export default router
